<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 7/28/17
 * Time: 11:06 AM
 */

namespace common\components\increment;


use common\models\workin\CarInvonceVerifyHuirong;
use common\models\workin\IdcardCheck;
use common\models\workin\MortgageSignupHuirong;
use yii\base\Component;
use yii\base\ErrorException;
use yii\httpclient\Client;

/**
 * Class Huirong
 * @package common\components\increment
 */
class Huirong extends Component
{
    /**
     * @var CarInvonceVerifyHuirong $verifyModel
     */
    public $verifyModel;

    /**
     * @var MortgageSignupHuirong $model1
     */
    public $model1;

    /**
     * @var IdcardCheck $model2
     */
    public $model2;

    public $pic_base64;

    public static function getPicType()
    {
        $arr = [
            '171'=>'户口本首页',
            '201' =>'户口本户主页',
            '231' =>'户口本本人页',
            '1111' => '车辆合格证',
            '1151' => '车辆注册登记证',
            '1951' => '二手车车辆注册登记证',
            '1101' => '购车发票',
            '1761' => '保单',
            '611' => '购房发票',
        ];
        return $arr;
    }

    public static function getUrl()
    {
        return Config::$huirong['url'].Config::$huirong['uri'];
    }

    /**
     * 1.8购车发票验证
     */
    public function carInvonceVerify()
    {
        if (!$this->verifyModel){
            throw new ErrorException("没有定义验证模型", 1070);
        }
        $data = [
            'grantKey' => $this->verifyModel->grant_key,
            'apiKey' => $this->verifyModel->api_key,
//            'businessCode' => '', // 业务落地行编码
//            'pic' => '', // 购车发票图片（BASE64Encoder转换为字符串，图片大小不超过1M） （二选一）
//            'picNetPath' => '', // 图片网络地址 （二选一）
//            'picType' => 'int', // 图片类型，详细见《图片类型说明》
            'callBack' => $this->verifyModel->call_back, // 贵司提供的回调函数
//            'orderReportId' => '' // 请求方需携带一个本次请求的唯一标识
        ];
        if ($this->verifyModel->business_code)$data['businessCode'] = $this->verifyModel->business_code;
        if ($this->verifyModel->pic)$data['pic'] = $this->pic_base64;
        if ($this->verifyModel->pic_net_path)$data['picNetPath'] = $this->verifyModel->pic_net_path;
        if ($this->verifyModel->pic_type)$data['picType'] = $this->verifyModel->pic_type;
        if ($this->verifyModel->order_report_id)$data['orderReportId'] = $this->verifyModel->order_report_id;
        $this->verifyModel->pic = "@uploads_url/huirong/".time().rand(1000,9999)."_".$this->verifyModel->pic->baseName.'.'.$this->verifyModel->pic->extension;
        $client = new Client();
        $response = $client->createRequest()
            ->setMethod('post')
            ->setUrl(self::getUrl())
            ->setData($data)
            ->send();
        return $response;
    }

    /**
     * 1.11抵押登记
     */
    public function mortgageSignup()
    {
        if (!$this->model1){
            throw new ErrorException("没有定义验证模型", 1070);
        }
        $data = [
            'grantKey' => $this->model1->grant_key,
            'apiKey' => $this->model1->api_key,
//            'businessCode' => '', // 业务落地行编码
//            'plateNumber' => 'must', // 车牌号
//            'vehicleNumber' => '', // 车架号
            'callBack' => $this->model1->call_back, // 贵司提供的回调函数
//            'orderReportId' => '' // 请求方需携带一个本次请求的唯一标识
        ];
        if ($this->model1->business_code)$data['businessCode'] = $this->model1->business_code;
        if ($this->model1->plate_number)$data['plateNumber'] = $this->model1->plate_number;
        if ($this->model1->vehicle_number)$data['vehicleNumber'] = $this->model1->vehicle_number;
        if ($this->model1->order_report_id)$data['orderReportId'] = $this->model1->order_report_id;
        $client = new Client();
        $response = $client->createRequest()
            ->setMethod('post')
            ->setUrl(self::getUrl())
            ->setData($data)
            ->send();
        return $response;
    }

    /**
     * 1.24身份证实名认证(带照)
     */
    public function idcardCheck0()
    {
        if (!$this->model2){
            throw new ErrorException("没有定义身份证实名认证模型", 1070);
        }
        $data = [
            'grantKey' => $this->model2->grant_key,
            'apiKey' => $this->model2->api_key,
//            'businessCode' => '', // 业务落地行编码
//            'plateNumber' => 'must', // 车牌号
//            'vehicleNumber' => '', // 车架号
//            'orderReportId' => '' // 请求方需携带一个本次请求的唯一标识
            'name' => $this->model2->name,
            'idCard' => $this->model2->id_card,
        ];
        if ($this->model2->business_code)$data['businessCode'] = $this->model2->business_code;
        if ($this->model2->order_report_id)$data['orderReportId'] = $this->model2->order_report_id;
        $client = new Client();
        $response = $client->createRequest()
            ->setMethod('post')
            ->setUrl(self::getUrl())
            ->setData($data)
            ->send();
        return $response;
    }
}